/* eslint-disable */
const strings = {
  isEmpty: function (str) {
    return str === undefined || str === null || str === '';
  },
  isNotEmpty: function (str) {
    return !this.isEmpty(str);
  },
  isBlank: function (str) {
    return str === undefined || str === 'undefined' || str === null || str === 'null' || /^\s*$/.test(str);
  },
  isNotBlank: function (str) {
    return !this.isBlank(str);
  },
  trim: function (str) {
    return str.replace(/^\s+|\s+$/, '');
  },
  // trim(str){ //删除左右两端的空格
  //     return str.replace(/(^\s*)|(\s*$)/g, "");
  // },
  ltrim: function (str) { //删除左边的空格
    return str.replace(/(^\s*)/g, "");
  },
  rtrim: function (str) { //删除右边的空格
    return str.replace(/(\s*$)/g, "");
  },
  //去除左右空格
  trimrl: function (str) {
    return (str+'').replace(/(^\s*)/g, "").replace(/(\s*$)/g, "");
  },
  trimByChar: function (str, char, type) {
    if (char) {
      if (type == 'left') {
        return str.replace(new RegExp('^\\' + char + '+', 'g'), '');
      } else if (type == 'right') {
        return str.replace(new RegExp('\\' + char + '+$', 'g'), '');
      }
      return str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
    }
    return str.replace(/^\s+|\s+$/g, '');
  },
  trimToEmpty: function (str) {
    return (str === undefined || str === null) ? "" : this.trim(str);
  },
  startsWith: function (str, prefix) {
    return str.indexOf(prefix) === 0;
  },
  endsWith: function (str, suffix) {
    let reg = new RegExp(suffix + '$')
    return reg.test(str)
  },
  //在字符串的左边填充一些特定的字符
  lpad: function (len, s, targetStr) {
    let a = new Array(targetStr);
    let n = (len - targetStr.length);
    for (let i = 0; i < n; i++) {
      a.unshift(s);
    }
    return a.join("");
  },
  //在字符串的右边填充一些特定的字符
  rpad: function (len, s, targetStr) {
    let a = new Array(targetStr);
    let n = (len - targetStr.length);
    for (let i = 0; i < n; i++) {
      a.push(s);
    }
    return a.join("");
  },
  cap: function (str) {
    if (this.isBlank(str)) {
      return str
    }
    return str.substring(0, 1).toUpperCase() + str.substring(1)
  },
  defaultString: function (str, defaultStr) {
    return (str === undefined || str === null || str == 'null') ? defaultStr : str
  },
  defaultIfBlank: function (str, defaultStr) {

    return this.isBlank(str) ? defaultStr : str
  },
  // 驼峰转下划线
  uncamelCase(oriName) {
    if(this.isBlank(oriName)){
      return oriName
    }
    return oriName.replace(/([A-Z])/g, '_$1').toLowerCase()
  },
  // 下划线转驼峰
  camelCase(name) {
    if(this.isBlank(name)){
      return name
    }
    return name.replace(/\_(\w)/g, (all, letter) => letter.toUpperCase())
  },
  // 中划线转驼峰
  hyphencamelCase(name) {
    if(this.isBlank(name)){
      return name
    }
    return name.replace(/\-(\w)/g, (all, letter) => letter.toUpperCase())
  },
  //返回字符串
  toStr(str) {
    if(this.isBlank(str)){
      return null
    }

    if(!isNaN(str)){
      return (str).toString()
    }
    return this.trim(str)
  },
  //获取字符串长度（汉字算两个字符，字母数字算一个）
  lengthByte(val) {
    if (!val) return 0
    var len = 0;
    for (var i = 0; i < val.length; i++) {
      var a = val.charAt(i);
      if (a.match(/[^\x00-\xff]/ig) != null) {
        len += 2;
      }
      else {
        len += 1;
      }
    }
    return len;
  },
  //转换成HTML显示的文本
  parseHTMLJSON(str) {
    //设置缩进为2个空格
    str = JSON.stringify(JSON.parse(str), null, 2)
    str = str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    return str
  },
  compare(op) {
    if (!op) {
        return op
    }
    if (!op.value1_style) {
        op.value1_style = "background-color:#FEC8C8;"
    }
    if (!op.value2_style) {
        op.value2_style = "background-color:#FEC8C8;"
    }
    if (!op.eq_min) {
        op.eq_min = 3
    }
    if (!op.eq_index) {
        op.eq_index = 5
    }
    if (!op.value1 || !op.value2) {
        return op
    }
    let ps = {
        v1_i: 0,
        v1_new_value: "",
        v2_i: 0,
        v2_new_value: ""
    }
    while (ps.v1_i < op.value1.length && ps.v2_i < op.value2.length) {
        if (op.value1[ps.v1_i] == op.value2[ps.v2_i]) {
            ps.v1_new_value += op.value1[ps.v1_i].replace(/</g,"<").replace(">",">")
            ps.v2_new_value += op.value2[ps.v2_i].replace(/</g,"<").replace(">",">")
            ps.v1_i += 1
            ps.v2_i += 1
            if (ps.v1_i >= op.value1.length) {
                ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2.substr(ps.v2_i).replace(/</g,"<").replace(">",">") + "</span>"
                break
            }
            if (ps.v2_i >= op.value2.length) {
                ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1.substr(ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>"
                break
            }
        } else {
            ps.v1_index = ps.v1_i + 1
            ps.v1_eq_length = 0
            ps.v1_eq_max = 0
            ps.v1_start = ps.v1_i + 1
            while (ps.v1_index < op.value1.length) {
                if (op.value1[ps.v1_index] == op.value2[ps.v2_i + ps.v1_eq_length]) {
                    ps.v1_eq_length += 1
                } else if (ps.v1_eq_length > 0) {
                    if (ps.v1_eq_max < ps.v1_eq_length) {
                        ps.v1_eq_max = ps.v1_eq_length
                        ps.v1_start = ps.v1_index - ps.v1_eq_length
                    }
                    ps.v1_eq_length = 0
                    break //只寻找最近的
                }
                ps.v1_index += 1
            }
            if (ps.v1_eq_max < ps.v1_eq_length) {
                ps.v1_eq_max = ps.v1_eq_length
                ps.v1_start = ps.v1_index - ps.v1_eq_length
            }

            ps.v2_index = ps.v2_i + 1
            ps.v2_eq_length = 0
            ps.v2_eq_max = 0
            ps.v2_start = ps.v2_i + 1
            while (ps.v2_index < op.value2.length) {
                if (op.value2[ps.v2_index] == op.value1[ps.v1_i + ps.v2_eq_length]) {
                    ps.v2_eq_length += 1
                } else if (ps.v2_eq_length > 0) {
                    if (ps.v2_eq_max < ps.v2_eq_length) {
                        ps.v2_eq_max = ps.v2_eq_length
                        ps.v2_start = ps.v2_index - ps.v2_eq_length
                    }
                    ps.v1_eq_length = 0
                    break //只寻找最近的
                }
                ps.v2_index += 1
            }
            if (ps.v2_eq_max < ps.v2_eq_length) {
                ps.v2_eq_max = ps.v2_eq_length
                ps.v2_start = ps.v2_index - ps.v2_eq_length
            }
            if (ps.v1_eq_max < op.eq_min && ps.v1_start - ps.v1_i > op.eq_index) {
                ps.v1_eq_max = 0
            }
            if (ps.v2_eq_max < op.eq_min && ps.v2_start - ps.v2_i > op.eq_index) {
                ps.v2_eq_max = 0
            }
            if ((ps.v1_eq_max == 0 && ps.v2_eq_max == 0)) {
                ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1[ps.v1_i].replace(/</g,"<").replace(">",">") + "</span>"
                ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2[ps.v2_i].replace(/</g,"<").replace(">",">") + "</span>"
                ps.v1_i += 1
                ps.v2_i += 1

                if (ps.v1_i >= op.value1.length) {
                    ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2.substr(ps.v2_i).replace(/</g,"<").replace(">",">") + "</span>"
                    break
                }
                if (ps.v2_i >= op.value2.length) {
                    ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1.substr(ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>"
                    break
                }
            } else if (ps.v1_eq_max > ps.v2_eq_max) {
                ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1.substr(ps.v1_i, ps.v1_start - ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>"
                ps.v1_i = ps.v1_start
            } else {
                ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2.substr(ps.v2_i, ps.v2_start - ps.v2_i).replace(/</g,"<").replace(">",">") + "</span>"
                ps.v2_i = ps.v2_start
            }
        }
    }
    op.value1 = ps.v1_new_value
    op.value2 = ps.v2_new_value
    return op
  },
  //join 不排除掉元素中的null
  joinWithNull(arr,separator) {
    if (!arr || arr.length < 1) return ''
    if(separator == undefined){
      separator = ",";
    }
    var str = "" + arr[0];
    for(var i = 1 ; i < arr.length ; i++){
      str = str + separator +arr[i];
    }
    return str;
  },
  //判断是否为字符串类型
  isString(str) {
    return (typeof str=='string')&&str.constructor==String
  },
  //判断是否全是字母
  isAllEn(str) {
    if (!str) return true
    return new RegExp("^[a-zA-Z]+$").test(str)
  },
  isJsonStr(str) {
    if (!str) return false
    if (typeof str == 'string') {
      try {
        var obj = JSON.parse(str);
        if (typeof obj == 'object' && obj) {
          return true;
        } else {
          return false;
        }

      } catch (e) {
        return false;
      }
    }
  },
  //分组
  funGroupBy(array, f) {
    let groups = {}
    array.forEach((o) => {
      let group = JSON.stringify(f(o))
      groups[group] = groups[group] || []
      groups[group].push(o)
    })
    return Object.keys(groups).map((group) => {
      return groups[group]
    })
  },
  /**
   * @Description：替换所有
   * @Date 2024-04-10
   * @param str       原始字符串
   * @param findStr   要查找的字符串
   * @param fillStr   要替换成啥字符串
  */
  replaceAll(str, findStr, fillStr) {
    if (!str) {
      return str
    }
    return str.replace(new RegExp(findStr,'g'), fillStr)
  },
  /**
   * @Description：替换br为换行
  */
  replaceBr(str) {
    if (!str) {
      return str
    }
    str = strings.replaceAll(str, '<br>', '\n')
    str = strings.replaceAll(str, '<br/>', '\n')
    str = strings.replaceAll(str, '<BR>', '\n')
    str = strings.replaceAll(str, '<BR/>', '\n')
    return str
  }
}

export default strings
